﻿Ext.define('app.prm.detail.DetailController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.detail',
    init: function () {
        var me = this;
    },
    onInit: function (view) {
        var me = this;


        App.AjaxGet('/api/services/app/permission/GetDetailPermissionOutPut?projectId=' + view.paramsId, function (data) {
            if (!data.result.hasView) {
                var tabPanel = view.up('maincenter');
                tabPanel.remove(view);
                me.redirectTo('app.prm.project.PrmApply');
            }
            else {
                view.getViewModel().setData(data.result);
                App.AjaxGet('/api/services/app/project/Get?id=' + view.paramsId, function (data2) {
                    console.log("*********************************");
                    console.log(data2.result);
                    console.log("*********************************");
                    view.getViewModel().setData(data2.result);
                });
            }
        });


    },
    onEditPhase: function (editor, context, ept) {
        var view = this.getView();
        var model = view.getViewModel();
        if (model.get('projectStatus') == "准备" || !model.get('hasEditPhase')) {
            return false;
        }
    },
    onTabActivate: function (tabPanel, newCard, oldCard, eOpts) {
        var view = this.getView();
        var model = view.getViewModel();
        var tabType = newCard.tabType;
        var hasEdit = false;
        var hasSave = false;

        if (tabType) {
            if (tabType == "basic") {
                hasEdit = true;
            }
            else {
                hasSave = true;
            }
        }
        model.set("hasBasicEdit", hasEdit);
        model.set("hasPhaseEdit", hasSave);
    },
    onSavePhase: function () {
        var view = this.getView();
        var phase = view.lookupReference("projectphase");
        var taskNote = view.lookupReference("projectTaskNote").getValue();

        App.AjaxPost('/api/services/app/project/SetProjectTaskNote', { 'projectId': view.paramsId, 'taskNote': taskNote }, function (data) { });

        phase.savePhase();
    },
    onDelProgress: function (grid, rowIndex, colIndex) {

        Ext.Msg.confirm("警告", "确定删除该条进展么？", function (btn) {
            if (btn == 'yes') {
                var record = grid.store.getAt(rowIndex);
                App.AjaxPost('/api/services/app/project/DelProgress?id=' + record.get('id'), null, function (data) {
                    grid.store.remove(record);
                });
            }
        })
    },
    onOpenPlan: function () {
        var view = this.getView();
        var model = view.getViewModel();
        var projectName = model.get('projectName');
        var projectCode = model.get('projectNo');
        window.open('/Gannt/Home/Index?projectId=' + encodeURI(view.paramsId) + "&name=" + encodeURI(projectName) + "&code=" + encodeURI(projectCode));
    },
    onFollowProject: function () {
        var view = this.getView();
        var model = view.getViewModel();
        App.AjaxPost('/api/services/app/project/FollowerProject?id=' + encodeURI(view.paramsId), null, function (data) {
            model.set('hasFollowered', true);
        });
    },
    onUnFollowProject: function () {
        var view = this.getView();
        var model = view.getViewModel();
        App.AjaxPost('/api/services/app/project/UnFollowerProject?id=' + encodeURI(view.paramsId), null, function (data) {
            model.set('hasFollowered', false);
        });
    },
    onAbInquirys: function () {
        var model = this.getViewModel();
        console.log(model);
        //显示相关询盘
        var win = Ext.create("Ext.window.Window",
            {
                title: '相关询盘',
                width: 600,
                height: 300,
                buttonAlign: 'center',
                layout: 'fit',
                items: [
                    {
                        xtype: 'grid',
                        forceFit: true,
                        store: Ext.create("app.ux.PageInquiryStore",
                                   {
                                       url: '/api/services/app/Inquiry/GetInquirys?productNo=' + model.get('productNo')
                                   }
                                   ),
                        columns: [
                        { header: '项目流水号', dataIndex: 'projectNo' },
                        { header: '九洲代号', dataIndex: 'inquiryNo' },
                         { header: '状态', dataIndex: 'statusStr' },
                         { header: '负责人', dataIndex: 'adminName' }
                        ],
                        listeners: {
                            rowdblclick: function (grid, record) {
                                var inquiryid = record.get('Id');
                                var win = Ext.create("app.prm.inquiry.view",
                                   {
                                       title: '询盘查看',
                                       inquiryId: record.get('inquiryId'),
                                       subId: record.get('subId')
                                   });
                                win.show();
                            }
                        }

                    }
                ]
            });
        win.show();

    },
    onEditInfo: function (panel, tool, event) {
        var view = this.getView();
        var win = Ext.create("app.prm.detail.EditInfo", { title: '编辑项目信息', projectId: view.paramsId });
        win.on("savesuccess", function (data) {
            this.onInit(view);
            win.close();
        }, this);
        win.show();
    },
    onEditCose: function (panel, tool, event) {
        var view = this.getView();
        var win = Ext.create("app.prm.detail.EditCost", { title: '编辑项目成本', projectId: view.paramsId });
        win.on("savesuccess", function (data) {
            this.onInit(view);
            win.close();
        }, this);
        win.show();
    },
    onTeamComment: function () {
        var view = this.getView();
        var model = view.getViewModel();

        var win = Ext.create("app.prm.detail.TeamEvaluation", {
            projectId: view.paramsId,
            teamCommentStatus: model.get('teamCommentStatus')
        });
        win.show();
    },
    onOpenDocument: function () {
        var view = this.getView();
        //var mainPanel = view.up('tabpanel');
        //mainPanel.newTab(view.paramsId, "app.prm.document.Index", "项目文档");
        var model = view.getViewModel();
        var projectNo = model.get("projectNo");
        var projectName = model.get("projectName");

        Ext.util.Cookies.set('projectdocument' + view.paramsId, projectName + "(" + projectNo + ")文档");
        this.redirectTo('app.prm.document.Index/' + view.paramsId);
    },
    onCMOProgress: function () {

        var view = this.getView();
        var model = view.getViewModel();
        var projectNo = model.get("projectNo");
        var projectName = model.get("projectName");

        Ext.util.Cookies.set('projectcmoprogress' + view.paramsId, projectName + "(" + projectNo + ")项目进度表");
        this.redirectTo('app.prm.detail.CMOProgress/' + view.paramsId); 
    },
    onDiscuss: function () {
        //项目提问
        var view = this.getView();
        //var mainPanel = view.up('tabpanel');
        //mainPanel.newTab(view.paramsId, "app.prm.detail.Discuss", "项目讨论");
        var model = view.getViewModel();
        var projectNo = model.get("projectNo");
        var projectName = model.get("projectName");

        Ext.util.Cookies.set('projectdiscuss' + view.paramsId, projectName + "(" + projectNo + ")讨论");
        this.redirectTo('app.prm.detail.Discuss/' + view.paramsId);
    },
    onOpenSynth: function () {
        var view = this.getView();
        var model = view.getViewModel();
        var projectNo = model.get("projectNo");
        var projectName = model.get("projectName");
        Ext.util.Cookies.set('projectsynth' + view.paramsId, projectName + "(" + projectNo + ")工艺路线");
        this.redirectTo('app.prm.synth.Index/' + view.paramsId);
    }
});
